﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Collections.ObjectModel;
using MReviewerClient.MReviewerSvc;
using MReviewerClient.Providers;
using System.Windows.Navigation;
using MReviewerClient.Models;
using System.Windows.Data;

namespace MReviewerClient.Views
{
    public partial class DetailEstablishmentView : PhoneApplicationPage
    {
        #region Fields

        /// <summary>
        /// The list of establishment
        /// </summary>
        public ObservableCollection<EstablishmentTypeDTO> EstablishmentTypes { get; set; }

        /// <summary>
        /// The list of FoodType
        /// </summary>
        public ObservableCollection<FoodTypeDTO> FoodTypes { get; set; }

        /// <summary>
        /// The selected Establishment
        /// </summary>
        public EstablishmentDTO SelectedEstablishment { get; set; }

        /// <summary>
        /// Selected Establishment Type index
        /// </summary>
        public int SelectedEstablishmentTypeIndex { get; set; }

        /// <summary>
        /// Selected Food Type index
        /// </summary>
        public int SelectedFoodTypeIndex { get; set; }

        #endregion

        #region Constructor

        /// <summary>
        /// The default constructor
        /// </summary>
        public DetailEstablishmentView()
        {
            InitializeComponent();
        }

        #endregion

        #region Methods & Handlers

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            string id = NavigationContext.QueryString["id"];
            this.SelectedEstablishment = EstablishmentModel.Instance.SelectByID(id);
            this.EstablishmentTypes = EstablishmentTypeModel.Instance.EstablishmentTypes;
            this.FoodTypes = FoodTypeModel.Instance.FoodTypes;

            this.SelectedEstablishmentTypeIndex = EstablishmentTypeModel.Instance.GetIndexFromID(this.SelectedEstablishment.EstablishmentType.ID);
            this.SelectedFoodTypeIndex = FoodTypeModel.Instance.GetIndexFromID(this.SelectedEstablishment.FoodType.ID);

            this.DataContext = this;
        }

        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            // Create memento object
            var memento = this.SelectedEstablishment.CreateShallowClone();

            // Update detail information
            this.SelectedEstablishment.ID = txtEstablishmentID.Text;
            this.SelectedEstablishment.IDEstablishmentType = (lstEstblismentType.SelectedItem as EstablishmentTypeDTO).ID;
            this.SelectedEstablishment.IDFoodType = (lstFoodType.SelectedItem as FoodTypeDTO).ID;
            this.SelectedEstablishment.Name = txtName.Text;
            this.SelectedEstablishment.Location = txtLocation.Text;

            // Should checking whether object has been modified before execute update
            if (this.SelectedEstablishment.IsNotModified(memento) == false)
            {
                Action actionInformResult = () =>
                {
                    // Checking the result
                    if (EstablishmentModel.Instance.UpdateResult)
                    {
                        // Update the source object
                        BindingExpression bindingExpression = null;

                        // Update Name
                        bindingExpression = txtName.GetBindingExpression(TextBox.TextProperty);
                        bindingExpression.UpdateSource();

                        // Update Location
                        bindingExpression = txtLocation.GetBindingExpression(TextBox.TextProperty);
                        bindingExpression.UpdateSource();

                        this.SelectedEstablishment.EstablishmentType = EstablishmentTypeModel.Instance.SelectByID(this.SelectedEstablishment.IDEstablishmentType);
                        this.SelectedEstablishment.FoodType = FoodTypeModel.Instance.SelectByID(this.SelectedEstablishment.IDFoodType);

                        MessageBox.Show("The new establishment has been updated", "Information", MessageBoxButton.OK);
                    }
                    else
                    {
                        MessageBox.Show("Cannot updated the new establishment!", "Error", MessageBoxButton.OK);
                    }
                };
                // Start to update information of specified establishment
                EstablishmentModel.Instance.Update(this.SelectedEstablishment, actionInformResult);      
            }
            else
            {
                MessageBox.Show("There is no any modification to update", "Information", MessageBoxButton.OK);
            }
        }

        private void menListAllReview_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri(string.Format("/Views/ListAllReviewView.xaml?id={0}", 
                this.SelectedEstablishment.ID), UriKind.RelativeOrAbsolute));  
        }

        private void menAddNewReview_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri(string.Format("/Views/AddReviewDetailView.xaml?id={0}",
                   this.SelectedEstablishment.ID), UriKind.RelativeOrAbsolute));
        }

        #endregion
    }
}